package com.example.bluejumplib;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.example.bluejumplib.usb.JumpUsbReceiver;
import com.example.bluejumplib.utils.JavaUtils;

import java.util.Set;

import felhr.usbservice.ADV_Callback;
import felhr.usbservice.UsbService;


public class JumpHandler {
    private static final String TAG = "JumpHandler";
    private JumpUsbReceiver mUsbReceiver = new JumpUsbReceiver();
    private Context context;
    private UsbService usbService;
    private ADV_Callback advCallback;

    public void init(ADV_Callback advCallback) {
        this.advCallback = advCallback;
    }

    //设置广播参数并监听
    public void startJumpService(Context context, ConnectInterface mConnectInterface) {
        if (usbService != null)
            return;
        if (this.context != null) {
            release();
        }
        mUsbReceiver.setConnectInterface(mConnectInterface);
        mUsbReceiver.notifyState();
        Log.i(TAG, "startJumpService: ");
        this.context = context;
        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbService.ACTION_USB_PERMISSION_GRANTED);
        filter.addAction(UsbService.ACTION_NO_USB);
        filter.addAction(UsbService.ACTION_USB_ATTACHED);
        filter.addAction(UsbService.ACTION_ADRECEIVER_READY);
        filter.addAction(UsbService.ACTION_ADRECEIVER_STOP);
        filter.addAction(UsbService.ACTION_USB_DISCONNECTED);
        filter.addAction(UsbService.ACTION_USB_NOT_SUPPORTED);
        filter.addAction(UsbService.ACTION_USB_PERMISSION_NOT_GRANTED);
        startService(UsbService.class, usbConnection, null);
        context.registerReceiver(mUsbReceiver, filter);
    }

    //与usb跳绳广播器服务建立连接绑定关系
    private final ServiceConnection usbConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            usbService = ((UsbService.UsbBinder) arg1).getService();
            usbService.setAdvCallback(advCallback, true);
            Boolean result = JavaUtils.getValueByPropName(usbService, "serialPortConnected", true);
            Log.e(TAG, "onServiceConnected: " + result);
            //没连接就直接返回
            if (!result)
                return;
            mUsbReceiver.state = true;
            mUsbReceiver.notifyState();
//            Toast.makeText(context, "USB state "+result, Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            Log.e(TAG, "onServiceDisconnected: ");
            usbService = null;
        }
    };

    //开启服务
    private void startService(Class<?> service, ServiceConnection serviceConnection, Bundle extras) {
        if (!UsbService.SERVICE_CONNECTED) {
            Intent startService = new Intent(context, service);
            if (extras != null && !extras.isEmpty()) {
                Set<String> keys = extras.keySet();
                for (String key : keys) {
                    String extra = extras.getString(key);
                    startService.putExtra(key, extra);
                }
            }
            context.startService(startService);
        }
        Intent bindingIntent = new Intent(context, service);
        context.bindService(bindingIntent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    public void release() {
        if (usbService == null)
            return;
        Log.i(TAG, "release: ");
        usbService = null;
        context.unregisterReceiver(mUsbReceiver);
        context.unbindService(usbConnection);
        if (UsbService.SERVICE_CONNECTED) {
            Log.e(TAG, "release:UsbServiceDisconnect  ");
            Intent startService = new Intent(context, UsbService.class);
            context.stopService(startService);
        }

    }
}
